package com.andnnl.pool;

/**
 * Created by chenss on 2021/6/23.
 */
public class Msg<T> {
    private IUser user;
    private String userName;//用户名或者是key
    private T data;
    private int priority;//如果是TwoQueue：优先级只有0和1,1优先
    private int retryTimes;//重试次数
    private int dataSize = 1;// data对象，代表的数量，用于统计当前msg数量(默认是1)

    public Msg() {
    }

    @Deprecated
    public Msg(IUser<T> user, T data) {
        this.data = data;
        this.user = user;
    }

    /**
     * 用于第一次就添加延迟消息
     *
     * @param userName
     * @param data
     */
    public Msg(String userName, T data, int dataSize) {
        this.data = data;
        this.userName = userName;
        this.dataSize = dataSize;
    }

    /**
     * 用于第一次就添加延迟消息
     *
     * @param userName
     * @param data
     */
    public Msg(String userName, T data) {
        this.data = data;
        this.userName = userName;
    }

    public Msg(T data) {
        this.data = data;
    }

    /**
     * 优先级
     *
     * @param data
     * @param priority 如果是TwoQueue：优先级只有0和1,1优先
     */
    public Msg(T data, int priority) {
        this.data = data;
        this.priority = priority;
    }

    /**
     * 优先级
     *
     * @param data
     * @param priority 如果是TwoQueue：优先级只有0和1,1优先
     */
    public Msg(T data, int priority, int dataSize) {
        this.data = data;
        this.priority = priority;
        this.dataSize = dataSize;
    }

    public static <T> Msg<T> build() {
        return new Msg();
    }

    /**
     * 用于第一次就添加延迟消息
     *
     * @param userName
     * @param data
     */
    public static <T> Msg<T> build(String userName, T data) {
        return new Msg(userName, data);
    }


    /**
     * 用于第一次就添加延迟消息
     *
     * @param userName
     * @param data
     * @param dataSize
     */
    public static <T> Msg<T> build(String userName, T data, int dataSize) {
        return new Msg(userName, data, dataSize);
    }

    public static <T> Msg<T> build(T data) {
        return new Msg(data);
    }

    /**
     * 优先级
     *
     * @param data
     * @param priority 如果是TwoQueue：优先级只有0和1,1优先
     */
    public static <T> Msg<T> build(T data, int priority) {
        return new Msg(data, priority);
    }

    /**
     * 优先级
     *
     * @param data
     * @param priority 如果是TwoQueue：优先级只有0和1,1优先
     */
    public static <T> Msg<T> build(T data, int priority, int dataSize) {
        return new Msg(data, priority, dataSize);
    }

    public T getData() {
        return data;
    }

    public Msg<T> setData(T data) {
        this.data = data;
        return this;
    }

    public IUser<T> getUser() {
        return user;
    }

    public Msg<T> setUser(IUser<T> IUser) {
        this.user = IUser;
        return this;
    }

    public int getPriority() {
        return priority;
    }

    /**
     * 设置优先级，如果是TwoQueue：优先级只有0和1,1优先
     *
     * @param priority
     */
    public Msg<T> setPriority(int priority) {
        this.priority = priority;
        return this;
    }

    /**
     * 返回并增加重试次数
     *
     * @return
     */
    public int incAndGetRetryTimes() {
        return ++retryTimes;
    }

    public int getRetryTimes() {
        return retryTimes;
    }

    public String getUserName() {
        return userName;
    }

    public Msg<T> setUserName(String userName) {
        this.userName = userName;
        return this;
    }

    public int getDataSize() {
        return dataSize;
    }

    public Msg<T> setDataSize(int dataSize) {
        this.dataSize = dataSize;
        return this;
    }
}
